package com.gitee.feizns.explore.data_structure.linked_list.doubl;

/**
 * @author feizns
 * @since 2019/10/9
 */
public class MyLinkedList {

    private Node head;

    private Node tail;

    private int size;

    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addAtHead(1);
        list.addAtTail(3);
        list.addAtIndex(1, 2);
        list.print();
        System.out.println(list.get(-1));
        list.deleteAtIndex(1);
        list.print();
        System.out.println(list.get(-3));
    }

    /** Initialize your data structure here. */
    public MyLinkedList() { }

    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
        if ( index >= 0 && index < size )
            return node(index).val;
        throw new IndexOutOfBoundsException();
    }

    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void addAtHead(int val) {
        addAtIndex(0, val);
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        addAtIndex(size, val);
    }

    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
        if ( isEmpty() && index <= 0 ) {
            head = new Node(val);
            tail = head;
            size++;
        } else if ( index <= 0 ) {
            Node second = head;
            head = new Node(val);
            second.prev = head;
            head.next = second;
            size++;
        } else if ( index == size ) {
            Node newTail = new Node(val);
            newTail.prev = tail;
            tail.next = newTail;
            tail = newTail;
            size++;
        } else if ( index < size ){
            Node node = new Node(val);
            Node afterNode = node(index);
            Node prev = afterNode.prev;
            prev.next = node;
            node.prev = prev;
            afterNode.prev = node;
            node.next = afterNode;
            size++;
        }
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if ( index == 0 && size == 1 ) {
            head = null;
            tail = null;
            size--;
        } else if ( index == 0 ) {
            Node oldHead = head;
            head = head.next;
            oldHead.next = null;
            head.prev = null;
            size--;
        } else if ( index == size - 1 ) {
            Node oldTail = tail;
            tail = tail.prev;
            oldTail.prev = null;
            tail.next = null;
            size--;
        } else if ( index >= 0 && index < size ){
            Node removeNode = node(index);
            Node prev = removeNode.prev;
            Node next = removeNode.next;
            removeNode.prev = null;
            removeNode.next = null;
            prev.next = next;
            next.prev = prev;
            size--;
        }
    }

    public Node node(int index) {
        Node curt = head;
        for (int i = 0; i < index; i++)
            curt = curt.next;
        return curt;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void print() {
        for (Node curt = head; curt != null; curt = curt.next)
            System.out.print(curt.val + ",");
        System.out.println();
    }

    class Node {

        int val;
        Node prev, next;

        public Node(int val) {
            this.val = val;
        }
    }

}
